package shiyi.repository;

import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.transaction.annotation.Transactional;
import shiyi.domain.Discuss;

import java.util.List;


public interface DiscussRepository extends JpaRepository<Discuss,Integer> {
    // 1、查询author非空的Discuss评论集合
    public List<Discuss> findByAuthorNotNull();

    // 2、根据文章id分页查询Discuss评论集合
    @Query("SELECT c FROM t_comment c WHERE  c.aId = ?1")
    public List<Discuss> getDiscussPaged(Integer aid,Pageable pageable);

    /**
     * 3、使用元素SQL语句，根据文章id分页查询Discuss评论集合
     * 有nativeQuery = true时，是可以执行原生sql语句，所谓原生sql，
     * 也就是说这段sql拷贝到数据库中，然后把参数值给一下就能运行了
     */
    @Query(value = "SELECT * FROM t_comment  WHERE  a_Id = ?1",nativeQuery = true)
    public List<Discuss> getDiscussPaged2(Integer aid, Pageable pageable);

    //4、 根据评论id修改评论作者author
    @Transactional
    @Modifying  //变更数据库
    @Query("UPDATE t_comment c SET c.author = ?1 WHERE  c.id = ?2")
    public int updateDiscuss(String author,Integer id);

    // 5、根据评论id删除评论
    @Transactional
    @Modifying
    @Query("DELETE FROM t_comment c WHERE c.id = ?1")
    public int deleteDiscuss(Integer id);

}
